Imports Microsoft.VisualBasic

Namespace My
    Partial Public Class MyPage
        Inherits Page

    End Class

    Partial Public Class MyPage
        Private auths As Data.DataRow = Nothing

        Private Sub LoadAuth()
            Dim dt As Data.DataTable = clsAuth.GetProgramAuths(Me.ProgramName)

            'MyObj.ShowMessage(Me.GetType()..Name)
            'MyObj.ShowMessage(Me.ProgramName)

            Me.AuthLoaded = True

            If dt.Rows.Count > 0 Then
                For Each dr As Data.DataRow In dt.Rows
                    For Each col As Data.DataColumn In dt.Columns
                        Dim authName As String = Text.RegularExpressions.Regex.Replace(col.ColumnName, "(\w+)Access", "$1")
                        Dim success As Boolean = authName <> col.ColumnName

                        If success = True Then
                            If dr(col.ColumnName) = "Y" Then
                                ViewState("Can" + authName) = True
                            Else
                                ViewState("Can" + authName) = False
                            End If
                        End If
                    Next

                Next
            End If

        End Sub

        Public Function CheckAuth(ByVal authName) As Boolean
            Dim authColumnName As String = authName + "Access"

            If ViewState("Can" + authName) = True Then
                Return True
            Else
                Return False
            End If
        End Function

        Public ReadOnly Property CanAdd() As Boolean
            Get
                If Me.AuthLoaded = False Then
                    Me.LoadAuth()
                End If

                Return Me.CheckAuth("Add")
            End Get
        End Property

        Public ReadOnly Property CanEdit() As Boolean
            Get
                If Me.AuthLoaded = False Then
                    Me.LoadAuth()
                End If

                Return Me.CheckAuth("Edit")
            End Get
        End Property

        Public ReadOnly Property CanDelete() As Boolean
            Get
                If Me.AuthLoaded = False Then
                    Me.LoadAuth()
                End If

                Return Me.CheckAuth("Delete")
            End Get
        End Property

        Public ReadOnly Property CanQuery() As Boolean
            Get
                If Me.AuthLoaded = False Then
                    Me.LoadAuth()
                End If

                Return Me.CheckAuth("Query")
            End Get
        End Property

        Public ReadOnly Property CanPrint() As Boolean
            Get
                If Me.AuthLoaded = False Then
                    Me.LoadAuth()
                End If

                Return Me.CheckAuth("Print")
            End Get
        End Property

        Public ReadOnly Property CanAdmin() As Boolean
            Get
                If Me.AuthLoaded = False Then
                    Me.LoadAuth()
                End If

                Return Me.CheckAuth("Admin")
            End Get
        End Property

        Public ReadOnly Property HaveAccess() As Boolean
            Get
                Return Me.CanAdd OrElse Me.CanDelete OrElse Me.CanEdit OrElse Me.CanPrint OrElse Me.CanQuery OrElse Me.CanAdmin
            End Get
        End Property

        Private Property AuthLoaded() As Boolean
            Get
                Return ViewState("AuthLoaded")
            End Get
            Set(ByVal value As Boolean)
                ViewState("AuthLoaded") = value
            End Set
        End Property

        Public Property ProgramName() As String
            Get
                If ViewState("ProgramName") Is Nothing Then
                    ViewState("ProgramName") = GetPageName()
                End If

                Return ViewState("ProgramName")
            End Get
            Set(ByVal value As String)
                ViewState("ProgramName") = value
            End Set
        End Property

        Public Shared Function GetPageName() As String
            Return System.IO.Path.GetFileNameWithoutExtension(HttpContext.Current.Request.PhysicalPath)
        End Function

        'Public Function GetNewStringBuilder() As MyAuthStringBuilder
        '    Return New MyAuthStringBuilder(Me.ProgramName)
        'End Function
    End Class
End Namespace
